package com.wujilee.wujileenetty.netty.io.channel;

import lombok.Data;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SelectableChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;

/**
 * @author liwuji
 * @date 2024/8/26
 * @desc
 **/
@Data
public class TimeClientHandle implements  Runnable{
    private String host;

    private int port;

    private Selector selector;

    private SocketChannel socketChannel;

    private volatile  boolean stop;

    public TimeClientHandle() {
        try {
            selector = Selector.open();
            socketChannel = SocketChannel.open();
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

    @Override
    public void run() {
        while (!stop){
            try {

                selector.select();
                Iterator<SelectionKey> selectionKeyIterator = selector.selectedKeys().iterator();
                SelectionKey selectionKey = null;
                while (selectionKeyIterator.hasNext()){
                    selectionKey = selectionKeyIterator.next();
                    try {
                        handleInput(selectionKey);

                    }catch (Exception e){
                        if(selectionKey != null){
                            selectionKey.cancel();
                            if(selectionKey.channel() != null){
                                selectionKey.channel().close();
                            }
                        }
                    }

                }
            }catch (Exception e){
                e.printStackTrace();
            }
            if(selector != null){
                try {
                    selector.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

    }

    private void handleInput(SelectionKey selectionKey) throws IOException {
        if(selectionKey.isValid()){
            SocketChannel channel =(SocketChannel) selectionKey.channel();
            if(selectionKey.isConnectable()){
                if(channel.finishConnect()){
                    channel.register(selector,SelectionKey.OP_READ);
//                    dowrite(channel);
                }
            }
            if(selectionKey.isReadable()){
                ByteBuffer buffer = ByteBuffer.allocate(1024);
                int read = channel.read(buffer);
                if(read >0){
                    buffer.flip();
                    byte[] aByte = new byte[buffer.remaining()];
                    buffer.get(aByte);
                    String string = new String(aByte, "UTF-8");
                    System.out.println("接收到来自服务器到消息: "+string);
                    this.stop = true;
                }else {
                    // 完成数据到读取有-1 此时关闭通道 这种读取不是一次读取  假如我发送10M的数据，-1 会在10m数据都读取完之后返回  这个过程中
                    if(read <0 ){

                    }
                }
            }

        }
    }

    public  void doConnect(){

    }
}
